<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>标签管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="30">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="tag">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加标签</el-button>
        </el-col>
      </el-row>
      <el-table :data="taglist" stripe>
        <el-table-column label="序号" prop="id"></el-table-column>
        <el-table-column label="标签名称" prop="tag"></el-table-column>
        <el-table-column label="创建时间" prop="create_time"></el-table-column>
        <el-table-column label="是否可用">
          <template v-slot:default="scope">
            <el-switch v-model="scope.row.state" @change="tagStateChange(scope.row.id)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="scope">
            <el-button type="primary" icon="el-icon-edit" @click="open_edit_tag(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deleteTag(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="size"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <el-dialog
        title="添加标签"
        :visible.sync="addDialogVisible"
        @close="addDialogClosed"
        width="50%">
        <!--内容主体区域-->
        <el-form :model="TagForm" :rules="TagRules" ref="TagFormRef">
            <el-form-item label="标签名称" prop="tagName">
              <el-input v-model="TagForm.tag"></el-input>
            </el-form-item>
        </el-form>
        <!--内容底部区域-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addTag()">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog
        title="修改标签"
        :visible.sync="editDialogVisible"
        @close="editDialogClosed"
        width="50%">
        <!--内容主体区域-->
        <el-form :model="editTagForm" :rules="editTagRules" ref="editTagFormRef">
            <el-form-item label="标签名称" prop="tagName">
              <el-input v-model="editTagForm.tagName"></el-input>
            </el-form-item>
        </el-form>
        <!--内容底部区域-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="edit_tag()">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      taglist: [],
      current: 0,
      size: 1,
      total: 1,
      addDialogVisible: false,
      editDialogVisible: false,
      TagForm: {
        tag: ''
      },
      TagRules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      },
      editTagForm: {
        id: '',
        tag: ''
      },
      editTagRules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      },
      tag_id: '',
      tag: ''
    }
  },
  created () {
    this.getTags()
  },
  methods: {
    handleCurrentChange (currentPage) {
      this.$axios.get('tag/getSomeTags', {
        params: {
          current: currentPage,
          size: 10
        }
      }).then(response => {
        // console.log(response.data)
        this.$set(this, 'taglist', response.data.data.records)
        this.total = response.data.data.total
        this.pageSize = response.data.data.size
        this.current = response.data.data.current
      }).catch(error => {
        console.log(error)
      })
    },
    open_edit_tag (id) {
      this.editDialogVisible = true
      this.tag_id = id
    },
    edit_tag () {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.post('/tag/updateTag', {
        id: this.tag_id,
        tag: this.editTagForm.tag
      }).then(response => {
        if (response.data.code !== '200') this.$message.error('修改失败')
        else this.$message.success('修改成功')
        this.getTags()
        this.editDialogVisible = false
      })
    },
    tagStateChange (id) {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.post('/tag/updateTagState', {
        tag_id: id
      }).then(response => {
        if (response.data.code !== '200') this.$message.error('修改失败')
        else this.$message.success('修改成功')
        // this.getTags()
      })
    },
    addDialogClosed () {
      this.$refs.TagFormRef.resetFields()
    },
    editDialogClosed () {
      this.$refs.editTagFormRef.resetFields()
    },
    deleteTag (id) {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.post('/tag/deleteTag', {
        tag_id: id
      }).then(response => {
        if (response.data.code !== '200') this.$message.error('删除失败')
        else this.$message.success('删除成功')
        this.getTags()
      })
    },
    getTags () {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.get('/tag/getSomeTags', {
        params: {
          current: 1,
          size: 20
        }
      }).then(response => {
        // console.log(response.data)
        this.$set(this, 'taglist', response.data.data.records)
        this.total = response.data.data.total
        this.size = response.data.data.size
        this.current = response.data.data.current
      }).catch(error => {
        console.log(error)
      })
    },
    addTag () {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.post('/tag/addTag', this.TagForm).then(response => {
        if (response.data.code !== '200') this.$message.error('添加失败')
        else this.$message.success('添加成功')
        this.getTags()
      })
      this.addDialogVisible = false
    },
    search () {
      if (this.tag !== '') {
        var token = sessionStorage.getItem('token')
        this.$axios.defaults.headers.Authorization = 'Bearer ' + token
        this.$axios.get('/tag/fuzzy', {
          params: {
            tag_name: this.tag
          }
        }).then(response => {
          this.$set(this, 'taglist', response.data.data)
          this.total = response.data.data.length
          this.size = response.data.data.length
          this.current = 1
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
